package com.chuanjiabao.wisdom.ui.activity.login;

import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.WindowManager;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.adapter.ApplicationState;
import com.chuanjiabao.wisdom.adapter.BaseFragmentAdapter;
import com.chuanjiabao.wisdom.databinding.ActivityInputPersionInfoBinding;
import com.chuanjiabao.wisdom.service.UpdateService;
import com.chuanjiabao.wisdom.ui.BaseActivity;
import com.chuanjiabao.wisdom.ui.fragment.InputPersionFirstFramgment;
import com.chuanjiabao.wisdom.ui.fragment.InputPersionSecondFragment;

public class InputPersionInfoActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    ActivityInputPersionInfoBinding binding;
    private Fragment fragment[] = new Fragment[2];
    private ApplicationState applicationState = new ApplicationState();
    int currentItem = 0;

    //上传头像服务
    private Intent mServiceIntent;
    private IntentFilter mIntentFilter;
    private UpdateService updateService;
    private ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            updateService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            updateService = ((UpdateService.MyBinder) service).getService();
        }
    };

    @Override
    protected void onStart() {
        super.onStart();
        bindService(mServiceIntent, conn, BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_input_persion_info);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().setStatusBarColor(getResources().getColor(R.color.F1F2530));
        }
        binding.setActivity(this);
        initView();
    }

    private void initView() {

        mServiceIntent = new Intent(this, UpdateService.class);
        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction(UpdateService.HEART_BEAT_ACTION);
        mIntentFilter.addAction(UpdateService.MESSAGE_ACTION);

        BaseFragmentAdapter fragmentAdapter = new BaseFragmentAdapter(getSupportFragmentManager());
        fragment[0] = new InputPersionFirstFramgment();
        fragment[1] = new InputPersionSecondFragment();
        fragmentAdapter.addData(fragment);
        binding.vpModule.setAdapter(fragmentAdapter);
        binding.vpModule.setCurrentItem(currentItem);
        binding.vpModule.addOnPageChangeListener(this);

        binding.tvModuleLeft.setOnClickListener(this);
        binding.tvModuleRight.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvModuleLeft:
                binding.tvModuleLeft.setBackgroundResource(R.drawable.module_draw_input_left_select);
                binding.tvModuleRight.setBackgroundResource(R.drawable.module_draw_input_right_no_select);
                binding.vpModule.setCurrentItem(0);
                break;
            case R.id.tvModuleRight:
                binding.tvModuleLeft.setBackgroundResource(R.drawable.module_draw_input_left_no_select);
                binding.tvModuleRight.setBackgroundResource(R.drawable.module_draw_input_right_select);
                binding.vpModule.setCurrentItem(1);
                break;
            default:
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }

    @Override
    public void onPageSelected(int position) {
        if (applicationState.isMainSliding() && position == 1) {
            applicationState.setMainSliding(false);
        }
        switch (position) {
            case 0:
                binding.tvModuleLeft.setBackgroundResource(R.drawable.module_draw_input_left_select);
                binding.tvModuleRight.setBackgroundResource(R.drawable.module_draw_input_right_no_select);
                break;
            case 1:
                binding.tvModuleLeft.setBackgroundResource(R.drawable.module_draw_input_left_no_select);
                binding.tvModuleRight.setBackgroundResource(R.drawable.module_draw_input_right_select);
                break;
            default:
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (!applicationState.isMainSliding() && state != ViewPager.SCROLL_STATE_IDLE) {
            applicationState.setMainSliding(true);
        }
    }

}
